#ifndef _CT_UART_H
#define _CT_UART_H

#include "ct_type.h"

// #define CT_USE_UART1

// UART buffer size

#define UART_BUFF_SIZE 255

// Enumeration for UART port numbers
typedef enum {
    CT_UART0_TIMER1 = (uint8_t)1,
    CT_UART0_TIMER3 = (uint8_t)2,
    CT_UART1_TIMER3 = (uint8_t)3,
    CT_UART2        = (uint8_t)4,
    CT_UART3        = (uint8_t)5,
    CT_UART0        = (uint8_t)6,
    CT_UART1        = (uint8_t)7
} ct_uart_port_t;

// Error codes for UART initialization
typedef enum {
    CT_UART_SUCCESS = 0,
    CT_UART_ERROR_NULL_POINTER,
    CT_UART_ERROR_INVALID_ID,
    // More error codes can be added here
} ct_uart_error_t;

typedef void(code *ct_uart_callback_t)(void);

/* UART configuration structure */
typedef struct {
    ct_uart_port_t id;            // UART port number
    uint8_t        buf_length;    // Expected length of data to receive
    uint8_t        counter;       // Number of counter
    //
    uint8_t *rx_buffer;     // Pointer to the receive buffer
    uint8_t  rx_index;      // Current index in the receive buffer
    uint8_t  rx_length;     // Expected length of data to receive
    uint16_t rx_timeout;    // Timeout counter
    //
    uint8_t *tx_buffer;    // Pointer to the transmit buffer
    uint8_t  tx_index;     // Current index in the transmit buffer
    uint8_t  tx_length;    // Expected length of data to transmit
    //
    uint8_t rx_bLock : 1;    // Receive lock flag
    uint8_t tx_bBusy : 1;    // Transmit busy status flag
    //
    uint8_t rx_bTimeout : 1;          // Receive timeout flag
    uint8_t rx_bOverflow : 1;         // Receive overflow flag
    uint8_t rx_bChecksumError : 1;    // Receive checksum error flag
    //
    // ct_uart_callback_t *rx_callback;    // Callback function pointer
} UART_t;

// Variable declarations
extern UART_t g_Uart0;
// extern UART_t g_Uart1;
//  Function declarations

void ct_uart_Open(uint32_t       u32SysClock,
                  ct_uart_port_t Port,
                  uint32_t       u32Baudrate);
void ct_uart_Init(UART_t *UARTx, uint32_t baudrate);

uint8_t ct_uart_ReceiveData(UART_t *UARTx);
void    ct_uart_IntReceiveBuff(UART_t *UARTx);

void ct_uart_SendData(UART_t *UARTx, uint8_t dat);
void ct_uart_SendString(UART_t *UARTx, uint8_t *str);

void ct_uart_SendBuff(UART_t *UARTx, uint8_t dat);
void ct_uart_ResetIntSendFlag(UART_t *UARTx);
//
void ct_uart_InterruptEnable(ct_uart_port_t id, uint8_t Status);
void ControlUARTInterrupts(UART_t *UARTx, unsigned char enable);

#endif    // __CT_UART_H__
